<?php

/**
 * uniBoard API Badword
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class api_badword extends class_api{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore				= NULL;
	
	/**
	 * Badword ID
	 *
	 * @var integer
	 */
	
	public $word_id					= 0;
	
	/**
	 * Badword Find
	 *
	 * @var string
	 */
	
	public $word_find				= '';
	
	/**
	 * Badword Replace
	 *
	 * @var string
	 */
	
	public $word_replace			= '';
		
	// ------------------------------------------------------------------------

	/**
	 * Initialise Api
	 *
	 * @param object $appCore
	 * @param integer $target_id
	 */
	
	function __construct( $appCore, $target_id = 0){
					
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Store Word ID
		$this -> word_id		= (integer) $target_id;
		
		// Autoload If Word ID is bigger than zero
		if ( $target_id > 0)
		{
			// Make select
			$appCore -> db -> query( 'SELECT *
			FROM ' . DB_PREFIX . 'badwords
			WHERE word_id = \'' . $this -> word_id . '\'
			LIMIT 1');
			
			// Word found
			if ( $word_result = $appCore -> db -> fetch_assoc())
			{
				$this -> data_loaded			= true;
				
				$this -> word_find	 			= $word_result['word_find'];
				$this -> word_replace 			= $word_result['word_replace'];
			}
			else
			{
				//give action an error
				$this -> setError( $appCore -> lang['acp_badword_not_found']);
			}
			
			// Free result
			$appCore -> db -> freeResult();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Save API data to DB
	 *
	 */
	
	function saveData(){
		
		// Do mysql query only when we have changes, and no errors
		if ( !empty( $this -> changes) && !$this -> error)
		{	
			// Insert?
			if ( $this -> word_id == 0)
			{
				// Insert new record
				$this -> appCore -> db -> insert( $this -> changes, 'badwords');
			
				// Store new Word id
				$this -> word_id = $this -> appCore -> db -> insertId();
			}
			else
			{
				// Update record
				$this -> appCore -> db -> update( $this -> changes, 'badwords', 'word_id = \'' . $this -> word_id . '\'');
			}
			
			// Flush Cache
			class_cache::flushCache( 'badwords');
			class_cache::flushCache( 'parsing_data');
			
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Delete API data to DB
	 *
	 */
	
	function deleteData(){
		
		// Delete data
		$this -> appCore -> db -> delete( 'badwords', 'word_id = \'' . $this -> word_id . '\'');
		
		// Flush Cache
		class_cache::flushCache( 'badwords');
		class_cache::flushCache( 'parsing_data');
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set word_find
	 *
	 * @param string $new_value
	 */
	
	function setWordFind( $new_value){
		
		// Not empty?
		if ( isset( $new_value[0]))
		{
			// Store in Api
			$this -> word_find = $new_value;
			
			// Too long?
			if ( mb_strlen( $new_value) > 255)
			{
				// Set Error
				$this -> setError( $this -> appCore -> lang['acp_badword_find_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['word_find'] = $this -> appCore -> db -> strEscape( $this -> word_find);
			}
		}
		else
		{
			// Set Error
			$this -> setError( $this -> appCore -> lang['acp_badword_find_empty']);
		}
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set word_replace
	 *
	 * @param string $new_value
	 */
	
	function setWordReplace( $new_value){
		
		// Not empty?
		if ( isset( $new_value[0]))
		{
			// Store in Api
			$this -> word_replace = $new_value;
			
			// Too long?
			if ( mb_strlen( $new_value) > 255)
			{
				// Set Error
				$this -> setError( $this -> appCore -> lang['acp_badword_replace_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['word_replace'] = $this -> appCore -> db -> strEscape( $this -> word_replace);
			}
		}
		else
		{
			// Set Error
			$this -> setError( $this -> appCore -> lang['acp_badword_replace_empty']);
		}
	
	}
		
}